﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Eleanor.DAL;
using SaturniLibrary.Utility;

namespace Eleanor.BLL
{
    public class SettingBLL
    {
        private static SettingBLL instance;
        public static SettingBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SettingBLL();
                }
                return instance;
            }
        }

        protected SettingDAL settingDAL = new SettingDAL();

        public int Add(dynamic setting)
        {
            return settingDAL.Insert(setting);
        }

        public int Update(dynamic setting)
        {
            return settingDAL.Update(setting);
        }

        public dynamic Get(int settingId)
        {
            return settingDAL.Select(settingId);
        }

        public List<dynamic> GetAll()
        {
            return settingDAL.SelectAll();
        }

        public dynamic Delete(int settingId)
        {
            return settingDAL.Delete(settingId);
        }


        #region Specia Key
        public string Get(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                return "";
            }
            foreach (dynamic item in settingDAL.SelectAll())
            {
                if (key.Equals(item.Key, StringComparison.OrdinalIgnoreCase))
                {
                    return item.Value;
                }
            }
            return "";
        }

        public int GetPageSizeOfPaintingList()
        {
            string strValue = Get("PageSizeOfPaintingList");
            if (string.IsNullOrWhiteSpace(strValue))
            {
                return 16;
            }

            int pageSize = 0;
            if (int.TryParse(strValue, out pageSize))
            {
                return pageSize;
            }
            return 16;
        }

        public string GetWebsiteDomain()
        {
            string domain = Get("WebsiteDomain");
            if(string.IsNullOrWhiteSpace(domain))
            {
                return "www.1st-canvas.com";
            }
            return domain;
        }

        public string GetWebsiteName()
        {
            return Get("WebsiteName");
        }

        public string GetWebsiteKeyword()
        {
            return Get("WebsiteKeyword");
        }

        public string GetWebsiteDescription()
        {
            return Get("WebsiteDescription");
        }

        public string GetGACode()
        {
            return Get("GACode");
        }

        public bool HandleWwwSubdomain()
        {
            return StringNormalize.IsTrueSynonym(Get("HandleWwwSubdomain"));
        }

        #endregion
    }
}
